import request from "@/utils/request";
import {EmployeeModel} from "@/model/EmployeeModel";
import {PagingModel} from "@/model/PagingModel";

export default class EmployeeApi {


    /**
     * 查询员工列表
     * @param searchKey
     * @param state
     * @param pageIndex
     * @param pageSize
     */
    public static listEmployee(searchKey: string, state: number, pageIndex: number, pageSize: number): Promise<PagingModel<EmployeeModel>> {
        return new Promise((resolve, reject) => {
            request({
                url: '/system/empl/list',
                method: 'get',
                params: {
                    state,
                    searchKey,
                    pageNum: pageIndex,
                    pageSize
                }
            }).then((res: any) => {
                if (res && res.code === 200) {
                    if (res?.rows) {
                        res.data = res.rows.map(item => new EmployeeModel().deserialize(item))
                    }
                    res.pageIndex = pageIndex
                    res.pageSize = pageSize
                    res.total = +res.total
                    resolve(res)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static saveEmployee(employee:EmployeeModel): Promise<EmployeeModel> {
        return new Promise((resolve, reject) => {
            request({
                url: `/system/empl/save`,
                method: 'POST',
                data: employee
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(new EmployeeModel().deserialize(res.data))
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static updateEmployee(employeeId: string, employee:EmployeeModel): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/system/empl/${employeeId}`,
                method: 'PUT',
                data: employee
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static deleteEmployee(employeeId: string): Promise<boolean> {
        return new Promise((resolve, reject) => {
            request({
                url: `/system/empl/${employeeId}`,
                method: 'DELETE'
            }).then((res: any) => {
                if (res.code === 200) {
                    resolve(true)
                } else {
                    reject(res)
                }
            }, ()=> {reject()})
        })
    }

    public static uploadAvatar(data) {
        return request({
            url: '/system/empl/avatar',
            method: 'post',
            data: data
        })
    }

    public static listByDepartId(deptId: string): Promise<any[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/system/empl/depart/${deptId}/list`,
                method: 'get',
            }).then((res: any) => {
                if (res && res.code === 200) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }

    public static listDepartByEmplId(emplId: string): Promise<any[]> {
        return new Promise((resolve, reject) => {
            request({
                url: `/system/empl/${emplId}/dept/list`,
                method: 'get',
            }).then((res: any) => {
                if (res && res.code === 200) {
                    resolve(res.data)
                } else {
                    reject(res)
                }
            }, () => {
                reject()
            })
        })
    }
}

